package antani.client.widgets;

import antani.client.GWTClient;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Widget;

public class PlaylistItem extends Widget {
	int track;
	String title;
	String artist;
	String album;
	boolean isPlaying;
	private int index;

	interface MyUiBinder extends UiBinder<DivElement, PlaylistItem> {
	}

	private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

	@UiField
	DivElement trackName;
	@UiField
	DivElement artistName;

	public PlaylistItem(int index, int track, String title, String album, String artist, boolean active) {
		this.track = track;
		this.title = title;
		this.artist = artist;
		this.album = album;
		this.index = index;

		setElement(uiBinder.createAndBindUi(this));
		sinkEvents(Event.ONCLICK);

		trackName.setInnerText(track + ". " + title);
		artistName.setInnerText(artist);
		
		setCurrent(active);
	}

	public static PlaylistItem fromLine(int index, String line, boolean active) {
		String[] array = line.split(";");
		return new PlaylistItem(index, Integer.parseInt(array[0]), array[1], array[2], array[3], active);
	}

	public void onBrowserEvent(com.google.gwt.user.client.Event event) {
		switch (DOM.eventGetType(event)) {
		case Event.ONCLICK:
			GWTClient.playerPage.jump(index);
		}
	}

	public void setCurrent(boolean b) {
		setStyleName(b ? "playlistitem_current" : "playlistitem");
	}
}
